<template>
  <el-alert
    v-show="isShowAlert"
    class="el_alert"
    title="登录成功"
    type="success"
    center
    show-icon
  />
  <div class="login">
    <div class="login_box">
      <h1 class="login_title">叩丁狼商品管理系统</h1>
      <el-form ref="formRef" :model="validateForm" class="demo-dynamic">
        <el-form-item
          class="input_item"
          size="large"
          prop="username"
          :rules="[
            {
              required: true,
              message: '请输入账号',
              trigger: 'change',
            },
          ]"
        >
          <el-input
            placeholder="请输入账号"
            :prefix-icon="UserFilled"
            v-model="validateForm.username"
          />
        </el-form-item>
        <el-form-item
          size="large"
          prop="password"
          :rules="{
            required: true,
            message: '请输入密码',
            trigger: 'change',
          }"
        >
          <el-input
            type="password"
            placeholder="请输入密码"
            :prefix-icon="Lock"
            v-model="validateForm.password"
          />
        </el-form-item>

        <el-button
          class="login_button"
          type="primary"
          @click="submitForm(formRef)"
          >登录</el-button
        >
      </el-form>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from "vue";
import type { FormInstance } from "element-plus";
import { Lock, UserFilled } from "@element-plus/icons-vue";
import { getUserTokenApi, getUserInfiApi } from "../request/api";
import Cookies from "js-cookie";
import { useRouter } from "vue-router";
import { useStore } from "vuex";
import { ElMessage } from "element-plus";

const formRef = ref<FormInstance>();
const validateForm = reactive<{
  password: string;
  username: string;
}>({
  password: "",
  username: "",
});
let isShowAlert = ref(false); // 是否显示弹出框
const router = useRouter(); // 创建router对象
const store = useStore(); // 创建vuex对象

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;

  formEl
    .validate()
    .then(valid => {
      if (valid) {
        // 发送请求拿到用户token
        getUserTokenApi({
          username: validateForm.username,
          password: validateForm.password,
        }).then(res => {
          if (res.code === 200) {
            console.log(res.message);
            // 将token值存起来
            Cookies.set("token", res.data.tokenHead + res.data.token, {
              expires: 3, // 设置过期时间
            });
            // 通过token拿到用户信息
            store.dispatch("getMenus").then(res => {
              ElMessage({
                type: "success",
                message: "登录成功",
              });
              router.push("/index");
            });
          } else {
            ElMessage({
              type: "error",
              message: res.message,
            });
          }
        });
      }
    })
    .catch(err => {}); // 不做处理
};
</script>

<style lang="less" scoped>
* {
  margin: 0;
  padding: 0;
}
.login {
  width: 100%;
  height: 100vh;
  background: #2d3a4b;
  .login_box {
    position: relative;
    width: 500px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    .login_title {
      color: #fff;
      margin-bottom: 20px;
    }
    // 修改自带样式
    :deep(.el-input__inner) {
      background: #283443;
    }
    :deep(.el-form-item) {
      margin-bottom: 30px;
    }
    .login_button {
      width: 100%;
      height: 40px;
    }
  }
}
.el_alert {
  position: absolute;
}
</style>
